package demo.utils;

public class MathUtil {
    public static void main(String[] args) {
        int test = 32;
//        String test = "101";

//        System.out.println(changeTenToTwo(test));
//        System.out.println(changeTwoToTen(changeTenToTwo(test)));
    }

    /**
     * 十进制转二进制
     * @param test
     * @return
     */
    public static int changeTwoToTen(String test){
        String[] strings = test.split("");
        int count = 0;
        int sum = 0;
        while (count < strings.length) {
            if (Integer.valueOf(strings[count]) == 1) {
                sum += Math.pow(2, strings.length-count-1 );
            }
            if (count == strings.length&&Integer.valueOf(strings[count]) == 1) {
                sum += 1;
            }
            count++;
        }
        return sum;
    }

    /**
     * 二进制转十进制
     * @param test
     * @return
     */
    public static String changeTenToTwo(int test){
        String temp = "";
        String result = "";
        while (test > 1) {
            if (test % 2 == 0) {
                temp = "0";
            } else {
                temp = "1";
            }
            test = test / 2;
            result = temp + result;
        }
        return "1"+result;
    }

}
